<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->


<dom-module id="shop-ripple-container">
  <template strip-whitespace>
    <style>
      :host {
        display: inline-block;
        position: relative;
      }

      paper-ripple {
        color: var(--app-accent-color);
      }
    </style>
    <slot></slot>
  </template>
  <script>

    class ShopRippleContainer extends Polymer.mixinBehaviors(
      [Polymer.PaperRippleBehavior], Polymer.Element) {

      static get is() { return 'shop-ripple-container'; }

      constructor() {
        super();
        this._isDown = false;
      }

      ready() {
        super.ready();
        this.addEventListener('focus', (e)=>this._onFocus(e), true);
        this.addEventListener('blur', (e)=>this._onBlur(e), true);
        Polymer.Gestures.addListener(this, 'down', this._rippleDown);
        Polymer.Gestures.addListener(this, 'up', this._rippleUp);
      }

      _onFocus(event) {
        // Prevent second ripple when clicking causes both focus and down.
        if (!this._isDown) {
          this._rippleDown(event);
        }
      }

      _onBlur(event) {
        this._rippleUp(event);
      }

      _rippleDown(event) {
        this._isDown = true;
        this.getRipple().downAction(event);
      }

      _rippleUp(event) {
        this._isDown = false;
        this.getRipple().upAction(event);
      }

    }

    customElements.define(ShopRippleContainer.is, ShopRippleContainer);

  </script>
</dom-module>
